﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BKO.Domain.Services;
using BKO.Domain.Repositories;
using BKO.Domain.Model;

namespace BKO.Services
{
	public class BaseService<IEntity> : IBaseService<IEntity> where IEntity : IBaseEntity
	{
		protected readonly IRepository<IEntity> Repository;
		
		public BaseService(IRepository<IEntity> repository)
		{
			Repository = repository;
		}

		public IEntity Get(int id)
		{
			return Repository.GetAll()
				.SingleOrDefault<IEntity>(m => m.ID == id);
		}

		public IEntity GetByName(string name)
		{
			return Repository.GetAll()
				.SingleOrDefault<IEntity>(m => m.Name == name);
		}

		public List<IEntity> GetAll()
		{
			return Repository.GetAll().ToList();
		}

		public IEntity Add(string name, string description)
		{
			IEntity entity = Repository.CreateEmpty();
			entity.Name = name;
			entity.Description = description;
			Repository.Add(entity);
			return entity;
		}

		public void Update(int id, string name, string description)
		{
			IEntity entity = Get(id);
			entity.Name = name;
			entity.Description = description;
			Repository.Update();
		}

		public void Delete(int id)
		{
			Repository.Delete(Get(id));
		}
	}
}
